unit RaCRCFile;

interface

uses
  SysUtils, Classes, UCommon, NativeXml, windows;

type
  TRaCRCFile = class(TComponent)
  private
    fFileName: string;
    fFileSize: Int64;
    fCRC: string;
    fFileVersion: string;
    fOnCRC: TNotifyEvent;
    procedure SetFileName(const Value: string);
  protected
    function GetFileSize(filename: string): Int64;
  public
    constructor Create(AOwner: TComponent); override;
    function GetCRCCode(AFileName: string; var AFileSize: Int64; var ACRC: string; var AVersion: string): Boolean;
  published
    property filename: string read fFileName write SetFileName;
    property FileSize: Int64 read fFileSize;
    property CRC: string read fCRC;
    property FileVersion: string read fFileVersion;
    property OnCRC: TNotifyEvent read fOnCRC write fOnCRC;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Rarnu Components', [TRaCRCFile]);
end;

{ TRaCRCFile }

constructor TRaCRCFile.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

end;

function TRaCRCFile.GetCRCCode(AFileName: string; var AFileSize: Int64;
  var ACRC, AVersion: string): Boolean;
var
  d: dword;
  x: TFixedFileInfo;
begin
  result := True;
  try
    x := FileInfo(AFileName);
    AFileSize := GetFileSize(AFileName);
    CalcCRC32(AFileName, d);
    ACRC := Format('%4.4X', [d]);
    AVersion := Format('%d.%d.%d.%d', [x.wFileVersionLS, x.wFileVersionMS, x.wProductVersionLS, x.wProductVersionMS]);
  except
    result := False;
  end;
  if Assigned(OnCRC) then
    OnCRC(Self);
end;

function TRaCRCFile.GetFileSize(filename: string): Int64;
var
  searchRec: TsearchRec;
begin
  if FindFirst(filename, 0, searchRec) = 0 then
    result := searchRec.Size
  else
    result := 0;
  SysUtils.FindClose(searchRec);
end;

procedure TRaCRCFile.SetFileName(const Value: string);
begin
  fFileName := Value;
  if not GetCRCCode(fFileName, fFileSize, fCRC, fFileVersion) then
  begin
    fFileSize := 0;
    fCRC := '';
    fFileVersion := '';
  end;
end;

end.

